package com.shg.web.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.BookType;
import com.shg.service.BookTypeService;
import com.shg.web.tools.BaseConstrant;
import com.shg.web.tools.FreeMarkerTool;

@Controller
public class BookTypeAction {

	private List<BookType> types;
	
	private Map<String, Object> typeMap;
	
	private String ftlName;
	
	private String  htmlName;
	
	private String typeId;
	
	private Map<String, String> mapNavigation;
	
	private BookType bookType;
	
	private String [] navigationType;
	
	@Resource BookTypeService bookTypeService;

	private String message;
	/**
	 * 书籍类别查询
	 * @return
	 */
	public String bookType(){
		try {
			types = bookTypeService.getAllTypeList(null);
			if(mapNavigation!=null)
				mapNavigation.clear();
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询数据类别出错";
			return "error";
		}
		return "bookType";
	}
	/**
	 * 书籍类别选择
	 * @return
	 */
	public String bookTypeForId(){
		try {
			types = bookTypeService.getSelectTypeList(typeId);
			mapNavigation = new LinkedHashMap<String, String>();
			if(typeId!=null && !typeId.equals("")&&types.size()>0){
				navigation(types.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询数据类型产生异常";
			return "error";
		}
		return "bookTypeForId";
	}
	
	public String typeList(){
		try {
			types = bookTypeService.getSelectTypeList(typeId);
			mapNavigation = new LinkedHashMap<String, String>();
			if(typeId!=null && !typeId.equals("")&&types.size()>0){
				navigation(types.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询数据的类型产生异常";
			return "error";
		}
		return "typeList";
	}
	/**
	 * 组装导航
	 * @param str
	 * @param type
	 */
	private void navigation(BookType type){
		if(type.getParent()!=null){
			navigation(type.getParent());
			//注意这里的位置一定不能颠倒
			mapNavigation.put(type.getParent().getId(), type.getParent().getName());
		}
	}
	/**
	 *生成静态HTML
	 * @return
	 */
	public String bookTypeMap(){
		try {
			types = bookTypeService.getAllTypeList(navigationType);
			typeMap=new HashMap<String, Object>();
			typeMap.put("typeMap", types);
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH, htmlName, typeMap);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="生成书籍类别发生异常";
			return "error";
		}
		return "bookTypeMap";
	}
	/**
	 * 添加书籍类型
	 * @return
	 */
	public String addType(){
		if(bookType!=null){
			try {
				bookType.setName(URLDecoder.decode(bookType.getName(), "UTF-8"));
				bookType.setRemark(URLDecoder.decode(bookType.getRemark(), "UTF-8"));
				bookTypeService.insert(bookType);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return typeList();
	}
	/**
	 * 弹出添加书籍类型的UI
	 * @return
	 */
	public String typeUI(){
		if(bookType!=null){
			if(bookType.getParent()!=null){
				try {
					if(bookType.getParent().getId().equals("null")&&bookType.getParent().getName().equals("null")){
						bookType.setParent(null);
					}else{
						bookType.getParent().setName(URLDecoder.decode(bookType.getParent().getName(), "UTF-8"));
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		
		return "typeUI";
	}
	/**
	 * 修改名称
	 * @return
	 */
	public String editNameUI(){
		try {
			bookType.setName(URLDecoder.decode(bookType.getName(), "UTF-8"));
			bookType.setRemark(URLDecoder.decode(bookType.getRemark(), "UTF-8"));
			if(bookType.getParent()!=null){
				if(bookType.getParent().getId().equals("")||bookType.getParent().getId()==null){
					bookType.setParent(null);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "editNameUI";
	}
	/**
	 * 修改名称 备注
	 * @return
	 */
	public String editName(){
		try {
			bookType.setName(URLDecoder.decode(bookType.getName(), "UTF-8"));
			bookType.setRemark(URLDecoder.decode(bookType.getRemark(), "UTF-8"));
			bookTypeService.updateType(bookType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return typeList();
	}
	
	public String headNavigation(){
		if(typeId==null || typeId.equals("")){
			bookType();
		}else{
			bookTypeForId();
		}
		return "headNavigation";
	}
	/**
	 * 生成导航
	 * @return
	 */
	public String headNavigationMap(){
		try {
			List<BookType> bookType=bookTypeService.getNavigaction(navigationType);
			typeMap=new LinkedHashMap<String, Object>();
			typeMap.put("typeMap", bookType);
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH, htmlName, typeMap);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="生成书籍类别发生异常";
			return "error";
		}
		return "headNavigationMap";
	}
	public List<BookType> getTypes() {
		return types;
	}

	public void setTypes(List<BookType> types) {
		this.types = types;
	}

	public Map<String, String> getMapNavigation() {
		return mapNavigation;
	}

	public void setMapNavigation(Map<String, String> mapNavigation) {
		this.mapNavigation = mapNavigation;
	}

	public String getFtlName() {
		return ftlName;
	}

	public void setFtlName(String ftlName) {
		this.ftlName = ftlName;
	}

	public String getHtmlName() {
		return htmlName;
	}

	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public BookType getBookType() {
		return bookType;
	}

	public void setBookType(BookType bookType) {
		this.bookType = bookType;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	public String[] getNavigationType() {
		return navigationType;
	}
	public void setNavigationType(String[] navigationType) {
		this.navigationType = navigationType;
	}
}
